package org.hxyjs.dao.impl;

import org.hxyjs.dao.SupplierorderDao;
import org.hxyjs.entity.Supplierorder;
import org.hxyjs.mapper.SupplierorderMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("supplierorderDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class SupplierorderDaoImpl implements SupplierorderDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private SupplierorderMapper supplierorderMapper;

    @Override
    public List<Supplierorder> getSupplierorderByPage(PageInfo pageInfo) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currPage", pageInfo.getCurrPage());
        map.put("pageSize", pageInfo.getPageSize());
        int itemCount = (int) supplierorderMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return supplierorderMapper.getSupplierorderByPage(map);
    }


    public List<Supplierorder> getAllSupplierorders() throws Exception {
        return supplierorderMapper.selectByExample(null);
    }

    @Override
    public Supplierorder getSupplierorderByUsno(String usno) throws Exception {
        return supplierorderMapper.selectByPrimaryKey(usno);
    }


    @Override
    public Integer deleteSupplierorderByUsno(String usno) {
        return supplierorderMapper.deleteByPrimaryKey(usno);
    }

    @Override
    public Integer updateSupplierorderByUsno(Supplierorder supplierorder) {

        return supplierorderMapper.updateByPrimaryKey(supplierorder);
    }

    @Override
    public Integer addSupplierorder(Supplierorder supplierorder) {
        supplierorder.setUsno(initId());
        return supplierorderMapper.insertSelective(supplierorder);
    }

    public String initId() {
        String maxUsno = supplierorderMapper.getMaxUsno();
        return GenNumTools.initId("17001202104261", 5, maxUsno);
    }

}